package com.erju.service.impl;

import cn.hutool.core.date.DateUtil;
import com.erju.dao.LabAppointmentMapper;
import com.erju.pojo.SlLabAppointment;
import com.erju.pojo.VO.LabReviewVo;
import com.erju.service.LabAppointmentService;
import com.erju.utils.PageHrlperUtils;
import com.erju.utils.Result;
import com.github.pagehelper.PageHelper;
import com.sun.deploy.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;

/**
 * 实验室预约信息
 *
 * @Description:
 * @author: erju
 * @date: 2022.03.29
 */
@Service
@Slf4j
public class LabAppointmentServiceImpl implements LabAppointmentService {
    @Autowired
    LabAppointmentMapper labAppointmentMapper;

    /***
     *实验室id查询实验室预约详情
     * @Author erju
     * @Date 19:16 2022/3/25
     * @Param [slLab]
     * @return com.erju.utils.Result<?>
     **/
    @Override
    public Result<?> selectLabDetails(SlLabAppointment slLabAppointment) {
        //查询该实验室已有的预约
        //如果为null则返回null
        log.info(slLabAppointment.toString());
        List<SlLabAppointment> slLabAppointments = labAppointmentMapper.teachSelectAppointmentByAppointment(slLabAppointment);
        int dateAppointment[] = new int[4];
        Date beginOfDay = DateUtil.beginOfDay(slLabAppointment.getAppointment_time());
        if (slLabAppointments.size() == 0) {
            return Result.success("成功", dateAppointment);
        } else {
            slLabAppointments.forEach(labAppointment -> {
                log.info(labAppointment.toString());
                if (labAppointment.getAppointment_time().getTime() == DateUtil.offsetHour(beginOfDay, 8).getTime()) {
                    dateAppointment[0] = labAppointment.getAppointment_flag();
                } else if (labAppointment.getAppointment_time().getTime() == DateUtil.offsetHour(beginOfDay, 10).getTime()) {
                    dateAppointment[1] = labAppointment.getAppointment_flag();
                } else if (labAppointment.getAppointment_time().getTime() == DateUtil.offsetHour(beginOfDay, 14).getTime()) {
                    dateAppointment[2] = labAppointment.getAppointment_flag();
                } else {
                    dateAppointment[3] = labAppointment.getAppointment_flag();
                }
            });
            return Result.success("成功", dateAppointment);
        }
    }


    /**
     * 添加实验室预约
     *
     * @param slLabAppointment
     * @return
     */
    @Override
    public Result<?> addAppointment(SlLabAppointment slLabAppointment) {
        Boolean result = labAppointmentMapper.insertAppointment(slLabAppointment);
        return Result.success("成功", result);
    }

    /**
     * 管理员查询预约信息并审批
     * 个人查询
     *
     * @param labReviewVo
     * @return
     */
    @Override
    public Result<?>  selectAppointmentByFlag(LabReviewVo labReviewVo) {
        if (labReviewVo.getAppointment_flag() == null) {
            labReviewVo.setAppointment_flag(1);
        }


        List<LabReviewVo> slLabAppointments = labAppointmentMapper.selectAppointmentByFlag(labReviewVo);
//        Date date = DateUtil.beginOfDay(new Date());
//        ArrayList<LabReviewVo> list = new ArrayList<LabReviewVo>();
//        //需要剔除掉已经过期得预约时间
//        for (LabReviewVo slLabAppointment : slLabAppointments) {
//            log.info("每一个预约信息===" + slLabAppointment.toString());
//            Date appointment_time = slLabAppointment.getAppointment_time();
//            //是否小于当前天开始时间，是则库中删除,时间是否为空，删除数据库
//            if (date.getTime() < appointment_time.getTime()) {
//                list.add(slLabAppointment);
//            } else {
//                slLabAppointment.setAppointment_flag(4);
//                labAppointmentMapper.updateAppointmentFlag(slLabAppointment);
//            }
//        }

        return PageHrlperUtils.PageResult(slLabAppointments, "个人查看type、user、flag条件查询成功");
    }

    @Override
    public Result<?> deleteLabAppointment(SlLabAppointment slLabAppointment) {
        Boolean flag = labAppointmentMapper.deleteLabAppointment(slLabAppointment);
        return Result.success("删除预约信息" + slLabAppointment.toString() + "成功", flag);
    }

    @Override
    public Result<?> updateAppointmentFlag(SlLabAppointment slLabAppointment) {
        //查询一下是否已有通过审批的实验室
        //更新实验室信息
        Boolean flag = labAppointmentMapper.updateAppointmentFlag(slLabAppointment);
        return Result.success("更新预约flag", flag);
    }


    @Override
    public Result<?> selectPersonalLabSeat(SlLabAppointment slLabAppointment) {
        //存放二位座位表
        int seat[][] = new int[6][6];
        List<SlLabAppointment> slLabAppointments = labAppointmentMapper.selectAppointmentByAppointment(slLabAppointment);
        if (slLabAppointments.size() == 0) {
            return Result.success("成功", seat);
        } else {
            for (SlLabAppointment labAppointment : slLabAppointments) {
                log.info(labAppointment.getAppointment_time() + "----" + labAppointment.getAppointment_time().getTime());
                log.info(slLabAppointment.getAppointment_time() + "----" + slLabAppointment.getAppointment_time().getTime());
                //需要加一个判空条件
                String[] site = labAppointment.getAppointment_site().split(",");
                seat[Integer.parseInt(site[0])][Integer.parseInt(site[1])] = labAppointment.getAppointment_flag();
            }
            return Result.success("成功", seat);
        }

    }

    /**
     * 根据传递过来的实验室预约id值查询该实验已经添加的学生
     *
     * @param lab_appointment_id
     * @return
     */
    @Override
    public Result<?> selectLabAppointmentStudent(Integer lab_appointment_id) {
        List<Integer> studentList = labAppointmentMapper.selectLabAppointmentStudent(lab_appointment_id);
        return Result.success("成功", studentList);
    }

    /**
     * 添加预约实验室学生
     *
     * @param lab_appointment_student_id_list
     * @return
     */
    @Override
    public Result<?> addLabAppointmentStudent(String lab_appointment_student_id_list, Integer appointment_id) {
        //预约实验室的学生数据清洗
        labAppointmentMapper.deleteLabAppointmentStudent(appointment_id);
        //处理接受的参数
        String[] students = lab_appointment_student_id_list.split(",");
        Arrays.stream(students).forEach(System.out::println);
        for (String student : students) {
            labAppointmentMapper.insertLabAppointmentStudent(appointment_id, Integer.valueOf(student));
        }
        return Result.success("成功", null);
    }
}
